﻿using System.IO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BestTrader.Cms.Admin.Controllers
{
    public class DownloadResult : ContentResult {
        private string fileName;
        private byte[] fileData;

        public DownloadResult(string fileName, byte[] fileData) {
            this.fileName = fileName;
            this.fileData = fileData;
        }

        public override void ExecuteResult(ActionContext context)
        {
            if (string.IsNullOrEmpty(this.fileName))
                throw new System.Exception("指定文件名");

            if (this.fileData == null)
                throw new System.Exception("下载文件丢失或是被移除");

            context.HttpContext.Response.Headers["Content-Disposition"] = $"attachment; filename={this.fileName}";
            ContentType = "application/octet-stream";
            context.HttpContext.Response.Body.Write(this.fileData);
            //base.ExecuteResult(context);
        }
    }
    public abstract class BaseController: Controller
    {
    }
}
